using FPLibrary;
using FrameOperateSync;
using UnityEngine;

namespace Pvp.Ecs
{
    /// <summary>
    /// 机关系统
    /// </summary>
    public class UnitGearSystem : ISystem
    {
        public override void OnStart()
        {
        }

        public override void OnFrameExecute(int frameIndex)
        {
            var gearUnits = Entities.GetComponents<IGearUnit>();
            foreach (var unit in gearUnits)
            {
                if (unit.IsDestroyed)
                    continue;
                if (!unit.IsOpened && unit.GearOpenTrigger != null)
                {
                    unit.IsOpened = unit.GearOpenTrigger(unit);
                }

                if (unit.IsOpened && unit.GearLogicTicker != null)
                {
                    unit.GearLogicTicker(unit,frameIndex);
                }

                if (unit.GearDestroyTrigger != null)
                {
                    unit.IsDestroyed = unit.GearDestroyTrigger(unit);
                    if (unit.IsDestroyed)
                    {
                        if (unit.TryGetCombineComponent<ILifeUnit>(out var x))
                        {
                            x.LifeValue = 0;
                        }
                    }
                }
            }
        }
        
       // private 
    }

    public class UnitControllerSystem : ISystem
    {
        public override void OnStart()
        {

        }

        public override void OnFrameExecute(int frameIndex)
        {
            var units = Entities.GetComponents<IController>();
            foreach (var unit in units)
            {
                unit.MoveAction();

                unit.SkillAction();
            }
        }
    }

    public class UnitInputSystem : ISystem
    {
        private IPlayerInput m_PlayerInput;
        public override void OnStart()
        {
            m_PlayerInput = Entities.GetSingleComponent<IPlayerInput>();
            
            RouterUtility.RegistRenderHander(OnRender1);
            RouterUtility.RegistRenderHander(OnRender2);
            RouterUtility.RegistRenderHander(OnRender3);
            RouterUtility.RegistRenderHander(OnRender4);
            RouterUtility.RegistRenderHander(OnRender5);
            RouterUtility.RegistRenderHander(OnRender6);
            
            Debug.LogError("jkfjkdkfkdd");
        }

        public override void OnFrameExecute(int frameIndex)
        {
            
        }


        [FrameRender((int)OperateType.fightOp_Move, typeof(MoveOperateData), "移动")]
        private void OnRender1(bool preFlag, int playerId, int frameIndex, IRenderData operateData)
        {
        }

        [FrameRender((int)OperateType.fightOp_Jump, typeof(JumpOperateData), "跳跃")]
        private void OnRender2(bool preFlag, int playerId, int frameIndex, IRenderData operateData)
        {
            
        }
        
        [FrameRender((int)OperateType.fightOp_Skill, typeof(SkillOperateData), "技能")]
        private void OnRender3(bool preFlag, int playerId, int frameIndex, IRenderData operateData)
        {
            var data = operateData as SkillOperateData;

            m_PlayerInput.skillIndexCode = data.skillIndex;
        }
        
        [FrameRender((int)OperateType.fightOp_Button, typeof(ButtonOperateData), "界面按钮")]
        private void OnRender4(bool preFlag, int playerId, int frameIndex, IRenderData operateData)
        {
            
        }
        
        [FrameRender((int)OperateType.fightOp_Stop, typeof(StopOperateData), "stop")]
        private void OnRender5(bool preFlag, int playerId, int frameIndex, IRenderData operateData)
        {
            
        }
        
        [FrameRender((int)OperateType.fightOp_Keyboard, typeof(KeyboardOperateData), "keyboard")]
        private void OnRender6(bool preFlag, int playerId, int frameIndex, IRenderData operateData)
        {
            var data = operateData as KeyboardOperateData;
            
            Debug.Log($"recv input{playerId},{data.keyCodePressdown} = ");

            m_PlayerInput.inputPlayerId = playerId;
            
            //移动
            if (data.keyCodePressdown == KeyCode.W)
            {
                m_PlayerInput.moveDir = FPVector2.up;
            }

            if (data.keyCodePressdown == KeyCode.A)
            {
                m_PlayerInput.moveDir = FPVector2.left;
            }
            if (data.keyCodePressdown == KeyCode.S)
            {
                m_PlayerInput.moveDir = FPVector2.down;
            }
            if (data.keyCodePressdown == KeyCode.D)
            {
                m_PlayerInput.moveDir = FPVector2.right;
            }

            if (data.keyCodePressdown == KeyCode.Space)
            {
                m_PlayerInput.moveDir = FPVector2.zero;
            }

            //技能
            if (data.keyCodePressdown == KeyCode.J)
            {
                m_PlayerInput.skillIndexCode = 1;
            }

            if (data.keyCodePressdown == KeyCode.K)
            {
                m_PlayerInput.skillIndexCode = 2;
            }
        }
    }
}